import React, { Component } from "react"
import InfoTable from "../common/sourceTable"
import { Card, Button, Popconfirm, message } from "antd"
import { FormInstance } from 'antd/lib/form'
import { EditFilled, DeleteFilled } from '@ant-design/icons'
import { getSuppInfo,addtSuppInfo,editSuppInfo,delSUppInfo } from "@net/company/supplier"
import DialogForm from '../common/dialogForm'
import Modal from '@comp/Modal'
import HeaderSearch from '../common/headerSearch'
import Power from '@comm/power'
import "../../index.scss"
import moment from "moment"

interface IPorps { }
interface IStates {
    data: any[]
    loading: boolean,
    popVisible: string,
    visible: boolean,
    title: string,
    delId: number,
    editId: number,
    roleSelectData: any[],
    page: number,
    pageSize: number,
    total: number
}
// 供应商管理
export default class Supplier extends Component<IPorps, IStates> {
    private dialogFormRef = React.createRef<FormInstance>() as any
    // 表格设置
    private column = [
        {
            key: "suName",
            dataIndex: "suName",
            title: "名称",
            align: "center"
        },
        {
            key: "suLeader",
            dataIndex: "suLeader",
            title: "负责人",
            align: "center"
        },
        {
            key: "suPhone",
            dataIndex: "suPhone",
            title: "联系电话",
            align: "center"
        },
        {
            key: "suPlace",
            dataIndex: "suPlace",
            title: "地址",
            align: "center",
        },
        {
            key: "suConStart",
            dataIndex: "suConStart",
            title: "合同生效时间",
            align: "center"
        },
        {
            key: "suConEnd",
            dataIndex: "suConEnd",
            title: "合同失效时间",
            align: "center"
        },
        {
            key: "do",
            dataIndex: "do",
            title: "操作",
            align: "center",
            render: (text: any, record: any) => {
                const item = record
                return (
                    <div className="do">
                        <Power authKey="busi:sup:update">
                            <Button size="small" type="primary" icon={<EditFilled />} onClick={() => { this.openEditDia(item) }} />
                        </Power>
                        <Power authKey="busi:sup:remove">
                            <Popconfirm title="您确定要删除吗？" okText="是" cancelText="否"
                                onConfirm={this.delete} visible={item.key === this.state.popVisible}
                                onCancel={() => { this.setState({ popVisible: "-1" }) }}>
                                <Button size="small" danger icon={<DeleteFilled />} onClick={() => { this.openPopDia(item) }} />
                            </Popconfirm>
                        </Power>
                    </div>
                )
            }
        }
    ]
    private dialogFormCol = () => {
        return [
            {
                type: 'input',
                label: "供应商名称",
                name: "suName",
                rules: [
                    { required: true, message: "请输入名称" }
                ],
                component: {
                    placeholder: "请输入名称",
                },
                style: { borderRadius: "5px" }
            },
            {
                type: 'input',
                label: "负责人",
                name: "suLeader",
                rules: [
                    { required: true, message: "请输入负责人" }
                ],
                component: {
                    placeholder: "请输入负责人",
                },
                style: { borderRadius: "5px" }
            },
            {
                type: 'input',
                label: "联系电话",
                name: "suPhone",
                rules: [
                    { required: true, message: "请输入联系电话" }
                ],
                component: {
                    placeholder: "请输入联系电话",
                },
                style: { borderRadius: "5px" }
            },
            {
                type: 'input',
                label: "公司地址",
                name: "suPlace",
                rules: [
                    { required: true, message: "请输入地址" }
                ],
                component: {
                    placeholder: "请输入地址",
                },
                style: { borderRadius: "5px" }
            },
            {
                type: 'datePicker',
                label: "合同生效时间",
                name: "suConStart",
                rules: [
                    { required: true, message: "请选择合同生效时间" }
                ],
                component: {
                    placeholder: "请选择合同生效时间",
                },
                style: { borderRadius: "5px",width:"300px" }
            },
            {
                type: 'datePicker',
                label: "合同失效时间",
                name: "suConEnd",
                rules: [
                    { required: true, message: "请选择合合同失效时间" }
                ],
                component: {
                    placeholder: "请选择合同失效时间",
                },
                style: { borderRadius: "5px",width:"300px" }
            },
        ]
    }
    private searchCol = [
        {
            type: 'input',
            name:"suname",
            label:"供应商",
            component: {
                placeholder: "请输入供应商名称",
            },
            style: { borderRadius: "5px",width:"350px" }
        }
    ]
    constructor(props: IPorps) {
        super(props)
        this.state = {
            data: [],
            loading: true,
            popVisible: "-1",
            visible: false,
            title: "",
            delId: 0,
            editId: 0,
            roleSelectData: [],
            page: 1,
            pageSize: 5,
            total: 0
        }
    }
    public componentDidMount() {
        this.fetchData(this.state.page, this.state.pageSize)
    }
    // 获取数据
    public fetchData = async (page: number, pageSize: number) => {
        const res = await getSuppInfo({ page, pageSize })
        res.data.data.map((item: any) => {
            return item.key = item.Id
        })
        this.setState({ loading: false, data: res.data.data, total: res.data.total })
    }
    public search = async (item:any) => {
        if (item.suname === undefined) {
            this.fetchData(this.state.page, this.state.pageSize)
            return
        }
        const res = await getSuppInfo({ page: 1, pageSize: 1 }, item.suname)
        const { status, msg, data,total } = res.data
        if (status === 203) {
            message.error({content:msg,style:{marginTop:"20%"}})
        } else {
            message.success({content:msg,style:{marginTop:"20%"}})
            this.setState({ data: data,total })
        }
    }
    // 页码切换监听事件
    public pageChange = (page: number, pageSize?: number) => {
        if (pageSize) {
            this.setState({ page, pageSize }, () => {
                this.fetchData(page, pageSize)
            })
        }
    }
    // 打开对话框
    public openAddDialog = () => {
        this.setState({ visible: true, title: "供应商信息添加" })
    }
    public openEditDia = (item: any) => {
        this.setState({ visible: true, title: "供应商信息编辑", editId: item.Id }, () => {
            item.suConStart = moment(item.suConStart, 'YYYY/MM/DD')
            item.suConEnd = moment(item.suConEnd, 'YYYY/MM/DD')
            this.dialogFormRef.current.setFieldsValue(item)
        })
    }
    public openPopDia = (item: any) => {
        this.setState({ popVisible: item.key, delId: item.Id })
    }
    // 点击对话框确认事件
    public onAddOk = () => {
        this.dialogFormRef.current.validateFields(this.add())
    }
    public onEditOk = () => {
        this.dialogFormRef.current.validateFields(this.edit())
    }
    public add = () => {
        const values = this.dialogFormRef.current.getFieldsValue()
        values.suConStart = moment(values.suConStart).format("YYYY/MM/DD")
        values.suConEnd = moment(values.suConEnd).format("YYYY/MM/DD")
        addtSuppInfo(values).then((res:any) => {
            const { msg, status } = res.data
            if (status === 203) {
                message.error({content:msg,style:{marginTop:"20%"}})
            } else if (status === 200) {
                message.success({content:msg,style:{marginTop:"20%"}})
                this.cancel()
            }
        })
    }
    public edit = async () => {
        const values = this.dialogFormRef.current.getFieldsValue()
        values.Id = this.state.editId
        values.suConStart = moment(values.suConStart).format("YYYY/MM/DD")
        values.suConEnd = moment(values.suConEnd).format("YYYY/MM/DD")
        const res = await editSuppInfo(values)
        const { status, msg } = res.data
        if (status === 203) {
            message.error({content:msg,style:{marginTop:"20%"}})
        } else if (status === 200) {
            message.success({content:msg,style:{marginTop:"20%"}})
            this.cancel()
        }
    }
    public delete = async () => {
        const res = await delSUppInfo(this.state.delId)
        const { status, msg } = res.data
        if (status === 203) {
            message.error({content:msg,style:{marginTop:"20%"}})
        } else if (status === 200) {
            message.success({content:msg,style:{marginTop:"20%"}})
            this.setState({ popVisible: "-1" })
            this.fetchData(this.state.page, this.state.pageSize)
        }
    }
    public cancel = () => {
        this.fetchData(this.state.page, this.state.pageSize)
        this.setState({ visible: false })
        this.dialogFormRef.current.resetFields()
    }
    public getContent = () => {
        return (
            <DialogForm ref={this.dialogFormRef} column={this.dialogFormCol()} go={() => { }}></DialogForm>
        )
    }
    public renderModal = (item?: string) => {
        return (<Modal
            title={this.state.title}
            visible={this.state.visible}
            content={this.getContent()}
            onOk={item === "" ? this.onAddOk : this.onEditOk}
            onCancel={this.cancel}></Modal>)
    }
    public renderButton() {
        return (
            <div>
                <Power authKey="busi:sup:save">
                    <Button type="primary" onClick={this.openAddDialog}>添加供应商</Button>
                </Power>
            </div>
        )
    }
    public render() {
        const { total } = this.state
        return (
            <div className="source">
                <Card className="card">
                    <HeaderSearch
                        authKey="busi:sup:list"
                        column={this.searchCol}
                        search={this.search}></HeaderSearch>
                    {this.renderButton()}
                    <InfoTable
                        columns={this.column}
                        loading={this.state.loading}
                        dataSource={this.state.data}
                        pageChange={this.pageChange}
                        total={total}>
                    </InfoTable>
                </Card>
                {this.state.title === "供应商信息添加" ? this.renderModal("") : this.renderModal()}
            </div>
        )
    }
}